package com.ltxtiyu.video.api;

import com.alibaba.dubbo.config.annotation.Reference;
import com.ltxtiyu.video.common.DataGridParam;
import com.ltxtiyu.video.service.app.TopicService;
import com.ltxtiyu.video.tool.ParamTool;
import com.ltxtiyu.video.tool.Tool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/apiTopic")
public class ApiTopic {
    public static final Logger LOGGER = LoggerFactory.getLogger(ApiTopic.class);

    @Reference
    TopicService topicService;

    /**
     * 话题首页
     * @param request
     * @return
     */
    @RequestMapping("/topicIndex")
    public String topicIndex(HttpServletRequest request){
        String userId = (String) request.getAttribute("userId");
        Map<String, String> params = ParamTool.requestToParamMap(request);
        DataGridParam dgp = Tool.genDataGridParam(request);
        Integer start = (dgp.getPage() - 1) * dgp.getRows();
        Integer rows = dgp.getRows();
        params.put("start", String.valueOf(start));
        params.put("rows", String.valueOf(rows));
        params.put("userId",userId);
        return topicService.topicIndex(params);
    }

    /**
     * 获取热门话题列表
     * @param request
     * @return
     */
    @RequestMapping("/listHotTopic")
    public String listHotTopic(HttpServletRequest request){
        String userId = (String) request.getAttribute("userId");
        Map<String, String> params = ParamTool.requestToParamMap(request);
        DataGridParam dgp = Tool.genDataGridParam(request);
        Integer start = (dgp.getPage() - 1) * dgp.getRows();
        Integer rows = dgp.getRows();
        params.put("start", String.valueOf(start));
        params.put("rows", String.valueOf(rows));
        params.put("userId",userId);
        return topicService.listHotTopic(params);
    }

    /**
     * 热门话题和热搜词
     * @param request
     * @return
     */
    @RequestMapping("/hotTopicAndHotWord")
    public String hotTopicAndHotWord(HttpServletRequest request){
        String userId = (String) request.getAttribute("userId");
        Map<String, String> params = ParamTool.requestToParamMap(request);
        params.put("userId",userId);
        return topicService.hotTopicAndHotWord(params);
    }

    /**
     * 话题首页
     * @param request
     * @return
     */
    @RequestMapping("/search")
    public String search(HttpServletRequest request){
        String userId = (String) request.getAttribute("userId");
        Map<String, String> params = ParamTool.requestToParamMap(request);
        DataGridParam dgp = Tool.genDataGridParam(request);
        Integer start = (dgp.getPage() - 1) * dgp.getRows();
        Integer rows = dgp.getRows();
        params.put("start", String.valueOf(start));
        params.put("rows", String.valueOf(rows));
        params.put("userId",userId);
        return topicService.search(params);
    }



}
